package com.ruoyi.im.config;

import com.ruoyi.im.utils.RouteQueueUtil;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 服务集群配置
 */
@Component
public class ServerClusterConfig {
    @Autowired
    private RabbitAdmin admin;
    private final String EXCHANGE = "server_msg_exchange";

    @Autowired
    private RouteQueueUtil routeQueueUtil;

    @PostConstruct
    public void init() {
        Queue queue = QueueBuilder.durable(queueName())
                .build();
        admin.declareQueue(queue);

        //绑定
        TopicExchange topicExchange = new TopicExchange(EXCHANGE);
        Binding with = BindingBuilder.bind(queue).to(topicExchange).with(routeQueueUtil.getRouteKey());
        admin.declareBinding(with);
    }

    @Bean
    public String queueName() {
        //初始化集群配置，集群使用ip+端口
        String quename = "server_queue_";
        String routekey = routeQueueUtil.getRouteKey();
        String que = quename + routekey;
        return que;
    }
}
